package com.jnxx.okr.controller;

import com.jnxx.basic.query.PageInfo;
import com.jnxx.basic.util.AjaxResult;
import com.jnxx.okr.domain.Alignment;
import com.jnxx.okr.domain.Okr;
import com.jnxx.okr.query.AlignmentQuery;
import com.jnxx.okr.service.IAlignmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/api/alignment")
public class AlignmentController {

    @Autowired
    public IAlignmentService alignmentService;

    /**
     * 接口：添加或修改
     * @param alignmentMap  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Map<String, Alignment> alignmentMap){
        Alignment alignment = alignmentMap.get("alignment");
        if( alignment.getId()!=null){
		alignmentService.update(alignment);
        }else{
                alignmentService.add(alignment);
	}
        return new AjaxResult();
       
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult del(@PathVariable("id") Long id){
        alignmentService.del(id);
        return new AjaxResult();
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        alignmentService.batchDel(ids);
        return new AjaxResult();
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")
    public Alignment findOne(@PathVariable("id")Long id) {
        return alignmentService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping
    public List<Alignment> findAll(){
        return alignmentService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageInfo<Alignment> page(@RequestBody AlignmentQuery query) {
        return alignmentService.page(query);
    }


    @PutMapping("/addAlignmentForOkr/{sourceId}")
    public AjaxResult addAlignmentForOkr(@RequestBody Map<String, List<Okr>> alignmentList, @PathVariable("sourceId") Long sourceId){
        List<Okr> okrList = alignmentList.get("alignmentList");
        alignmentService.addAlignmentForOkr(okrList, sourceId);

        return new AjaxResult();
    }
}
